package com.server.test.service;

import java.io.UnsupportedEncodingException;
import org.json.JSONException;
import org.json.JSONObject;

import com.server.test.util.WeixinUtil;

public class UserInfoService {

	public static final String APP_ID = "wx0692fa375d68142f";
	public static final String APP_SECRET = "889ba2cae0b27043dbcf3b5520c05a5a";
	
	public static String getOpenId(String code) throws JSONException {
		
		String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={APP_ID}&secret={APP_SECRET}&code={CODE}code&grant_type=authorization_code";  
        requestUrl = requestUrl.replace("{APP_ID}", urlEncodeUTF8(APP_ID));  
        requestUrl = requestUrl.replace("{APP_SECRET}", urlEncodeUTF8(APP_SECRET)); 
        requestUrl = requestUrl.replace("{CODE}", urlEncodeUTF8(code));
        
        JSONObject jsonObject = WeixinUtil.httpRequest(requestUrl, "GET", null); 
        System.out.println(jsonObject.toString());
        /**
         * 
        {
            "access_token": "OezXcEiiBSKSxW0eoylIeAsR0GmYd1awCffdHgb4fhS_KKf2CotGj2cBNUKQQvj-oJ9VmO-0Z-_izfnSAX_s0aqDsYkW4s8W5dLZ4iyNj5Y6vey3dgDtFki5C8r6D0E6mSVxxtb8BjLMhb-mCyT_Yg",
            "expires_in": 7200,
            "refresh_token": "OezXcEiiBSKSxW0eoylIeAsR0GmYd1awCffdHgb4fhS_KKf2CotGj2cBNUKQQvj-oJ9VmO-0Z-_izfnSAX_s0aqDsYkW4s8W5dLZ4iyNj5YBkF0ZUH1Ew8Iqea6x_itq13sYDqP1D7ieaDy9u2AHHw",
            "openid": "oLVPpjqs9BhvzwPj5A-vTYAX3GLc",
            "scope": "snsapi_base"
        }
         */
        

//        https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID
        
        
		String openId = jsonObject.getString("openid");
		 
        //获取用户信息

		return openId;
        
	}
	
	
	public static JSONObject getUserInfo(String accessToken,String openId ) {

		String requestUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={ACCESS_TOKEN}&openid={OPENID}";  
        requestUrl = requestUrl.replace("{ACCESS_TOKEN}", urlEncodeUTF8(accessToken));  
        requestUrl = requestUrl.replace("{OPENID}", urlEncodeUTF8(openId)); 
        
        JSONObject jsonObject = WeixinUtil.httpRequest(requestUrl, "GET", null);
       /**
        * 
        {
            "subscribe": 1,
            "openid": "oLVPpjqs2BhvzwPj5A-vTYAX4GLc",
            "nickname": "XX",
            "sex": 1,
            "language": "zh_CN",
            "city": "深圳",
            "province": "广东",
            "country": "中国",
            "headimgurl": "http://wx.qlogo.cn/mmopen/JcDicrZBlREhnNXZRudod9PmibRkIs5K2f1tUQ7lFjC63pYHaXGxNDgMzjGDEuvzYZbFOqtUXaxSdoZG6iane5ko9H30krIbzGv/0",
            "subscribe_time": 1386160805
        }
        */
        return jsonObject;
	}
	
	
	public static String getApplicationAccessToken() {

		return null;
		
	}
	

	 
    private static String urlEncodeUTF8(String source) {  
        String result = source;  
        try {  
            result = java.net.URLEncoder.encode(source, "UTF-8");  
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        }  
        return result;  
    }  
    

}
